package database

import (
	"cache-go/datastruct/dict"
	"cache-go/interface/redis"
	"cache-go/protocol"
	"strings"
)

const (
	dataDictSize = 1 << 16
	ttlDictSize  = 1 << 10
)

type DB struct {
	index  int
	data   *dict.ConcurrentDict
	ttlMap *dict.ConcurrentDict
}

func makeDB() *DB {
	return &DB{
		data:   dict.MakeConcurrent(dataDictSize),
		ttlMap: dict.MakeConcurrent(ttlDictSize),
	}
}

func (db *DB) Exec(c redis.Connection, cmdLine [][]byte) redis.Reply {

	return db.execNormalCommand(cmdLine)
}

func (db *DB) execNormalCommand(cmdLine [][]byte) redis.Reply {
	cmdName := strings.ToLower(string(cmdLine[0]))
	cmd, ok := cmdTable[cmdName]
	if !ok {
		return protocol.MakeErrorReply("Error: undefined command " + cmdName)
	}
	return cmd.executor(db, cmdLine[1:])
}
